<template>
  <div class="main-container">
    <div class="main-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>等级规则管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="wrap">
      <div class="content">
        <div class="search-container">
          <el-form class="search-form" :model="form" ref="form" label-width="90px" label-position="right" inline>
            <el-row>
              <el-form-item label="规则名称" prop="gradeName">
                <el-input v-model="form.gradeName"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSearch">查询</el-button>
                <el-button @click="onReset">重置</el-button>
              </el-form-item>
            </el-row>
          </el-form>
        </div>
        <div class="mt-20">
          <el-button type="primary" @click="onAdd"><i class="el-icon-plus"></i> 新增等级规则</el-button>
          <!--
          <el-button type="danger" @click="onRemove" disabled><i class="el-icon-delete"></i> 批量删除</el-button>
          -->
        </div>
        <div class="mt-10">
          <el-table :data="data" v-loading="loading">
            <!-- <el-table-column type="selection" width="55" fixed>
            </el-table-column> -->
            <el-table-column prop="gradeName" label="规则名称" min-width="130" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="watchMinTime" label="视频查看最小时长(分钟)" min-width="120" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="watchMaxTime" label="视频查看最大时长(分钟)" min-width="120" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="操作"  width="300" fixed="right">
              <template slot-scope="scope">
                <el-button type="text" @click="doWatch(scope.row)" v-if="scope.row.gradePhoto">pc头像</el-button>
                <el-button type="text" @click="doPhoneWatch(scope.row)" v-if="scope.row.phoneGradePhoto">手机头像</el-button>
                <el-button type="text" @click="doEdit(scope.row.gradeRuleId)">修改</el-button>
                <el-button type="dangertext" @click="doDelete(scope.row.gradeRuleId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 
        <div class="mt-10 t-right" v-if="total > 0">
          <el-pagination
            background
            :current-page="currentPage"
            :total="total"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            >
          </el-pagination>
        </div>
        -->
      </div>
    </div>
    <el-dialog :title="title" width="300px" style="text-align: center;" :visible.sync="visible">
      <img :src="gradePhoto" width="100" alt="">
    </el-dialog>
    
    <el-dialog :title="title" width="300px" style="text-align: center;" :visible.sync="phoneVisible">
      <img :src="phoneGradePhoto" width="100" alt="">
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        currentPage: 1,
        pageSize: 10,
        form: {},
        data: [],
        title: '',
        visible: false,
        phoneVisible: false,
        gradePhoto: '',
        phoneGradePhoto: '',
        showTotal: false,
        total: 0,
        loading: false
      }
    },
    activated() {
      this.loadData()
    },
    created() {
      this.loadData()
    },
    watch: {
      
      $route () {
        this.currentPage = 1
        this.loadData()
      }
    },
    methods: {
      doWatch(data) {
        this.title = data.gradeName
        this.gradePhoto = data.gradePhoto
        this.visible = true;
      },
      doPhoneWatch(data){
        this.title = data.gradeName
        this.phoneGradePhoto = data.phoneGradePhoto
        this.phoneVisible = true;
      },
      loadData() {
        this.loading = true
        const params ={
          ...this.form,
          // currentPage: this.currentPage,
          // pageSize: this.pageSize,
        }
        this.$api.grade.getList(params).then(res => {
          if(res.success){
            this.data = res.data
            this.total = res.data.total
            this.loading = false
          }else {
            this.loading = false
          }
        }).catch(err => {
          this.loading = false
        })
      },
      onSearch() {
        this.currentPage = 1
        this.loadData()
      },
      onReset() {
        this.$refs.form.resetFields()
        this.currentPage = 1
        this.loadData()
      },
      onAdd() {
        this.$router.push('/grade/add')
      },
      onRemove() {
        
      },
      // 修改
      doEdit(id) {
        this.$router.push('/grade/edit/' + id)
      },
      // 删除
      doDelete(id) {
        this.$confirm('删除后不可还原，确认删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.$api.grade.delete(id).then(res => {
            if(res.success) {
              this.$message.success('已删除')
              this.currentPage = 1
              this.loadData()
            }
          })
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.currentPage = 1
        this.loadData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.loadData()
      }
    }
  }
</script>

<style>

 
</style>
